package com.wetest.uia2.stub;

import androidx.test.uiautomator.UiDevice;

import com.googlecode.jsonrpc4j.JsonRpcServer;

import fi.iki.elonen.NanoHTTPD;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import uiautomator.InstrumentShellWrapper;

/* loaded from: classes9.dex */
public class AutomatorHttpServer extends NanoHTTPD {
    private Map<String, JsonRpcServer> router;

    public AutomatorHttpServer(int port) {
        super(port);
        this.router = new HashMap<>();
    }

    public void route(String uri, JsonRpcServer rpc) {
        this.router.put(uri, rpc);
    }

    @Override // fi.iki.elonen.NanoHTTPD
    public NanoHTTPD.Response serve(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files) {
        ByteArrayInputStream is;
        Log.d(String.format("URI: %s, Method: %s, params, %s, files: %s", uri, method, params, files));
        if ("/stop".equals(uri)) {
            stop();
            return newFixedLengthResponse("Server stopped!!!");
        }
        if ("/ping".equals(uri)) {
            return newFixedLengthResponse("pong");
        }
        if ("/screenshot/0".equals(uri)) {
            float scale = 1.0f;
            if (params.containsKey("scale")) {
                try {
                    scale = Float.parseFloat(params.get("scale"));
                } catch (NumberFormatException e) {
                }
            }
            int quality = 100;
            if (params.containsKey("quality")) {
                try {
                    quality = Integer.parseInt(params.get("quality"));
                } catch (NumberFormatException e2) {
                }
            }
            File f = new File(InstrumentShellWrapper.getInstance().getTargetContext().getFilesDir(), "screenshot.png");
            UiDevice.getInstance(null).takeScreenshot(f, scale, quality);
            try {
                return newChunkedResponse(NanoHTTPD.Response.Status.OK, "image/png", new FileInputStream(f));
            } catch (FileNotFoundException e3) {
                Log.e(e3.getMessage());
                return newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "Internal Server Error!!!");
            }
        }
        if (this.router.containsKey(uri)) {
            JsonRpcServer jsonRpcServer = this.router.get(uri);
            if (params.get("NanoHttpd.QUERY_STRING") != null) {
                is = new ByteArrayInputStream(params.get("NanoHttpd.QUERY_STRING").getBytes());
            } else if (files.get("postData") != null) {
                is = new ByteArrayInputStream(files.get("postData").getBytes());
            } else {
                return newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "Invalid http post data!");
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                jsonRpcServer.handleRequest(is, os);
                return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/json", new ByteArrayInputStream(os.toByteArray()), os.size());
            } catch (IOException e4) {
                return newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "Internal Server Error!!!");
            }
        }
        return newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not Found!!!");
    }
}